package com.biz.primus.ms.member.api;

import com.biz.primus.model.member.enums.AddressTypes;
import com.biz.primus.model.member.enums.InvoiceTypes;
import com.biz.primus.model.member.vo.*;
import com.biz.primus.model.member.vo.request.*;
import com.biz.primus.model.member.vo.response.*;
import com.biz.primus.ms.base.api.BaseApiController;
import com.biz.primus.ms.member.dao.ro.MemberRo;
import com.biz.primus.ms.member.service.*;
import com.biz.primus.ms.member.trans.MemberRoToMemberVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liujun
 * @date 2017/9/27
 */
@RestController
@RequestMapping("soa/memberService/")
@Api("会员相关接口文档")
public class MemberApiController extends BaseApiController {

    private final MemberService memberService;

    private final MemberFavouriteService memberFavouriteService;
    private final MemberFootprintService memberFootprintService;
    private final MemberBankCardService memberBankCardService;
    private final MemberInvoiceService memberInvoiceService;
    private final MemberAuthTokenService memberAuthTokenService;
    private final MemberAddressService memberAddressService;
    private final NoticeService noticeService;

    @Autowired
    public MemberApiController(MemberService memberService, MemberFavouriteService memberFavouriteService,
                               MemberFootprintService memberFootprintService, MemberBankCardService memberBankCardService,
                               MemberInvoiceService memberInvoiceService, MemberAuthTokenService memberAuthTokenService,
                               MemberAddressService memberAddressService, NoticeService noticeService)
    {
        this.memberService = memberService;
        this.memberFavouriteService = memberFavouriteService;
        this.memberFootprintService = memberFootprintService;
        this.memberBankCardService = memberBankCardService;
        this.memberInvoiceService = memberInvoiceService;
        this.memberAuthTokenService = memberAuthTokenService;
        this.memberAddressService = memberAddressService;
        this.noticeService = noticeService;
    }

    @ApiOperation("账号注册")
    @PostMapping("register")
    public MemberRegisterResponseVo register(@RequestBody MemberRegisterRequestVo reqVo)
    {
        return memberService.register(reqVo);
    }

    @ApiOperation("账号登录")
    @PostMapping("login")
    public MemberLoginResponseVo login(@RequestBody MemberLoginRequestVo reqVo, @RequestParam("enableAuthToken") Boolean enableAuthToken)
    {
        return memberService.login(reqVo, enableAuthToken);
    }

    @ApiOperation("手机号验证码登录 没账号则注册 默认密码手机号后六位")
    @PostMapping("smsCodeLogin")
    public MemberLoginResponseVo smsCodeLogin(@RequestBody MemberSmsCodeLoginRequestVo reqVo, @RequestParam("enableAuthToken") Boolean enableAuthToken)
    {
        return memberService.smsCodeLogin(reqVo, enableAuthToken);
    }

    /**
     * 自动登录
     * @param reqVo
     */
    @ApiOperation("自动登录")
    @PostMapping("autologin")
    public MemberLoginResponseVo autologin(@RequestBody BaseRequestVo reqVo, @RequestParam("enableAuthToken") Boolean enableAuthToken)
    {
        return memberService.autologin(reqVo, enableAuthToken);
    }

    /**
     * 修改密码
     * @param reqVo
     */
    @ApiOperation("修改密码")
    @PostMapping("changePassword")
    public void changePassword(@RequestBody MemberChangePasswordRequestVo reqVo)
    {
        memberService.changePassword(reqVo);
    }

    /**
     * 重置密码
     * @param reqVo
     */
    @ApiOperation("重置密码")
    @PostMapping("resetPassword")
    public void resetPassword(@RequestBody MemberResetPasswordRequestVo reqVo)
    {
        memberService.resetPassword(reqVo);
    }

    /**
     * 根据用户id查询用户信息，返回数据和登录一样
     * @param reqVo
     */
    @ApiOperation("根据用户id和渠道编码查询用户信息，返回数据和登录一样")
    @GetMapping("getUserInfo")
    public MemberLoginResponseVo getUserInfo(@RequestBody BaseRequestVo reqVo, @RequestParam("enableAuthToken") Boolean enableAuthToken)
    {
        //todo 需要增加登录信息的修改保存
        return memberService.findMemberLoginResponseVoById(reqVo.getGp().getMemberId(), enableAuthToken);
    }

    /**
     * 根据用户id查询用户信息
     * @param memberId
     */
    @ApiOperation("根据用户id查询会员信息")
    @GetMapping("getUserInfoById")
    public MemberVo getUserInfoById(@RequestParam("memberId") Long memberId)
    {
        MemberRo ro = memberService.findRoById(memberId);
        return new MemberRoToMemberVo().apply(ro);
    }

    /**
     * 根据用户手机号码查询用户信息
     * @param mobile
     */
    @ApiOperation("根据用户手机号码查询用户信息")
    @GetMapping("getUserInfoByMobile")
    public MemberVo getUserInfoByMobile(@RequestParam("mobile") String mobile)
    {
        MemberRo ro = memberService.findRoByMobile(mobile);
        return new MemberRoToMemberVo().apply(ro);
    }

    /**
     * 更新个人信息(会员头像更新，生日更新,性别更新,昵称更新,真实姓名绑定，手机号码绑定，邮箱绑定）
     * @param reqVo
     */
    @ApiOperation("更新个人信息(会员头像更新，生日更新,性别更新,昵称更新,真实姓名绑定，手机号码绑定，邮箱绑定）")
    @PostMapping("updatePersonalInfo")
    public void updatePersonalInfo(@RequestBody UpdateMemberPersonalInfoRequestVo reqVo)
    {
        memberService.updatePersonalInfo(reqVo);
    }

    /**
     * 更新用户头像
     * @param reqVo
     */
    @ApiOperation("更新用户头像")
    @PostMapping("updateAvatar")
    public void updateAvatar(@RequestBody UpdateMemberAvatarRequestVo reqVo)
    {
        UpdateMemberPersonalInfoRequestVo vo = new UpdateMemberPersonalInfoRequestVo();
        vo.setGp(reqVo.getGp());
        vo.setPortraitUrl(reqVo.getImage());
        memberService.updatePersonalInfo(vo);
    }

    @ApiOperation("根据账号匹配会员信息")
    @GetMapping("findByAccount")
    public MemberVo findByAccount(@RequestParam("account") String account) {
        MemberRo ro = memberService.findRoByAccount(account);
        MemberVo vo = new MemberVo();
        if (ro != null) {
            BeanUtils.copyProperties(ro, vo);
        }
        //访问oms 设置首单id 和 是否有首单奖励
//        vo.setFirstOrderId();
//        vo.setNotFirstOrder();
        return vo;
    }

    @ApiOperation("根据手机号码查询会员")
    @GetMapping("findByMobile")
    public MemberVo findByMobile(@RequestParam("mobile") String mobile)
    {
        MemberRo ro = memberService.findRoByMobile(mobile);
        MemberVo vo = new MemberVo();
        if (ro != null) {
            BeanUtils.copyProperties(ro, vo);
        }
        //访问oms 设置首单id 和 是否有首单奖励
//        vo.setFirstOrderId();
//        vo.setNotFirstOrder();
        return vo;
    }

    //********************************************
    //               足迹end
    //********************************************
    /**
     * 分页获取用户足迹
     * @param reqVo
     */
    @ApiOperation("分页获取用户足迹")
    @GetMapping("findFootprint")
    public PageVo findFootprint(@RequestBody BasePageableRequestVo reqVo) { return memberFootprintService.findPageMemberFootprint(reqVo.getGp().getMemberId(), reqVo.getPage(), reqVo.getSize()); }

    /**
     * 记录一次用户足迹
     * @param reqVo
     */
    @ApiOperation("记录一次用户足迹")
    @PostMapping("recordFootprint")
    public void recordFootprint(@RequestBody MemberFootprintVo reqVo) { memberFootprintService.addMemberFootprint(reqVo);}

    /**
     * 清除用户足迹
     * @param reqVo
     */
    @ApiOperation("清除用户足迹")
    @PostMapping("clearFootprint")
    public void clearFootprint(@RequestBody BaseRequestVo reqVo) { memberFootprintService.cleanMemberFootprint(reqVo.getGp().getMemberId());}
    //********************************************
    //               足迹start
    //********************************************

    //********************************************
    //               优惠卷start
    //********************************************
    /**
     * 分页获取个人中心优惠券列表
     * @param reqVo
     */
    @ApiOperation("分页获取个人中心优惠券列表")
    @GetMapping("couponList")
    public PageVo couponList(@RequestBody MemberCouponRequestVo reqVo) { return null; }

    /**
     * 优惠卷详情
     * @param reqVo
     */
    @ApiOperation("优惠卷详情")
    @GetMapping("getCouponDetail")
    public MemberCouponCodeVo getCouponDetail(@RequestBody MemberCouponCodeRequestVo reqVo) { return null; }
    //********************************************
    //               优惠卷end
    //********************************************


    //********************************************
    //               银行卡start
    //********************************************
    /**
     * 根据用户id获取银行卡信息
     * @param reqVo
     */
    @ApiOperation("根据用户id获取银行卡信息")
    @GetMapping("getBankcardInfo")
    public MemberBankCardVo getBankcardInfo(@RequestBody BaseRequestVo reqVo) { return memberBankCardService.findVoByMemberIdOnLastUse(reqVo.getGp().getMemberId()); }

    /**
     * 更新银行卡信息
     * @param reqVo
     */
    @ApiOperation("更新银行卡信息")
    @PostMapping("updateBankcardInfo")
    public void updateBankcardInfo(@RequestBody MemberBankCardVo reqVo) { memberBankCardService.save(reqVo);}
    //********************************************
    //               银行卡end
    //********************************************


    //********************************************
    //               发票start
    //********************************************
    /**
     * 用户中心里 获取所有发票信息
     * @param memberId
     */
    @ApiOperation("用户中心里 获取所有发票信息")
    @GetMapping("getUserInvoices")
    public List<MemberInvoiceVo> getUserInvoices(@RequestParam("invoiceType") InvoiceTypes invoiceType, @RequestParam("memberId") Long memberId) { return memberInvoiceService.findVosByMemberId(invoiceType, memberId); }

    /**
     * 保存或新增发票
     * @param reqVo
     */
    @ApiOperation("保存或新增发票")
    @PostMapping("saveUserInvoice")
    public void saveUserInvoice(@RequestBody MemberInvoiceVo reqVo) { memberInvoiceService.save(reqVo);}

    /**
     * 删除发票
     *  @param invoiceId
     */
    @ApiOperation("删除发票")
    @PostMapping("deleteOneInvoice")
    public void deleteOneInvoice(@RequestParam("invoiceId") Long invoiceId) { memberInvoiceService.delById(invoiceId);}

    /**
     * 根据id获取发票信息
     * @param invoiceId
     */
    @ApiOperation("根据id获取发票信息")
    @GetMapping("findOrderInvoiceById")
    public MemberInvoiceVo findOrderInvoiceById(@RequestParam("invoiceId") Long invoiceId) { return memberInvoiceService.findVoById(invoiceId); }
    //********************************************
    //               发票end
    //********************************************

    //********************************************
    //               收藏start
    //********************************************
    /**
     * 添加收藏
     * @param reqVo
     */
    @ApiOperation("添加收藏")
    @PostMapping("addFavourite")
    public void addFavourite(@RequestBody ProductCodeRequestVo reqVo)
    {
        memberFavouriteService.toggleMemberFavouriteProduct(reqVo.getGp().getMemberId(), reqVo.getProductCode());
    }

    /**
     * 收藏列表
     * @param reqVo
     */
    @ApiOperation("收藏列表")
    @GetMapping("favouriteList")
    public PageVo favouriteList(@RequestBody BasePageableRequestVo reqVo)
    {
        PageVo vo = new PageVo();
        vo.setItems(memberFavouriteService.findByMemberFavouriteProduct(reqVo.getGp().getMemberId(), reqVo.getPage(), reqVo.getSize()));
        vo.setPage(reqVo.getPage());
        vo.setTotal(memberFavouriteService.findByMemberFavouriteProductTotals(reqVo.getGp().getMemberId()));
        return vo;
    }

    /**
     * 清空收藏
     * @param reqVo
     */
    @ApiOperation("清空收藏")
    @PostMapping("clearFavourite")
    public void clearFavourite(@RequestBody BaseRequestVo reqVo)
    {
        memberFavouriteService.cleanMemberFavouriteProduct(reqVo.getGp().getMemberId());
    }

    /**
     * 查询收藏商品的总数
     * @param reqVo
     */
    @ApiOperation("查询收藏商品的总数")
    @GetMapping("getFavouriteProductTotal")
    public Long getFavouriteProductTotal(@RequestBody BaseRequestVo reqVo) { return memberFavouriteService.findByMemberFavouriteProductTotals(reqVo.getGp().getMemberId()); }

    /**
     * 批量删除收藏商品
     * @param reqVo
     */
    @ApiOperation("批量删除收藏商品")
    @PostMapping("deleteFavouriteProducts")
    public void deleteFavouriteProducts(@RequestBody DeleteFavouriteProductsRequestVo reqVo)
    {
        memberFavouriteService.delete(reqVo.getGp().getMemberId(), reqVo.getProductCodes());
    }
    //********************************************
    //               收藏end
    //********************************************


    //********************************************
    //               token end
    //********************************************
    /**
     * 生成新的token并保存
     * @param memberId 会员id
     * @return 生成的token
     */
    @ApiOperation("生成新的token并保存")
    @PostMapping("generateNewToken")
    public String generateNewToken(@RequestParam("memberId") Long memberId) { return memberAuthTokenService.generateNewToken(memberId); }

    /**
     * 授权,不通过抛出code 1919或1909异常 客户端将做强退处理
     * @param token
     * @param memberId
     */
    @ApiOperation("授权,不通过抛出code 1919或1909异常 客户端将做强退处理")
    @PostMapping("authenticate")
    public void authenticate(@RequestParam("token") String token, @RequestParam("memberId") Long memberId) { memberAuthTokenService.authenticate(token, memberId);}

    /**
     * 刷新token(使用旧token鉴权并创建新token)
     * @param oldToken
     * @param memberId
     * @return 返回新的token
     */
    @ApiOperation("刷新token(使用旧token鉴权并创建新token)")
    @PostMapping("refreshToken")
    public String refreshToken(@RequestParam("oldToken") String oldToken, @RequestParam("memberId") Long memberId) { return memberAuthTokenService.refreshToken(oldToken, memberId); }

    /**
     * 根据用户id查询登录token
     * @param memberId
     */
    @ApiOperation("根据用户id查询登录token")
    @GetMapping("findTokenByUserId")
    public String findTokenByUserId(@RequestParam("memberId") Long memberId) { return memberAuthTokenService.findTokenByMemberId(memberId); }
    //********************************************
    //               token end
    //********************************************

    //********************************************
    //               address start
    //********************************************
    /**
     * 更新或保存地址标签
     * 标签名重复时会抛出异常
     */
    @ApiOperation("更新或保存地址标签")
    @PostMapping("saveOrUpdateLabel")
    public void saveOrUpdateLabel(@RequestBody MemberAddressCheckLabelNameRequestVo reqVo)
    {
        memberAddressService.saveOrUpdateLabel(reqVo);
    }

    /**
     * 获取用户所有的标签
     */
    @ApiOperation("获取用户所有的标签")
    @GetMapping("getAllUserLabel")
    public List<MemberAddressLabelVo> getAllUserLabel(@RequestParam("memberId") Long memberId)
    {
        return memberAddressService.getAllUserLabel(memberId);
    }

    /**
     * 删除标签
     */
    @ApiOperation("删除标签")
    @PostMapping("deleteLabelNew")
    public List<MemberAddressLabelVo> deleteLabelNew(@RequestBody MemberAddressIdRequestVo reqVo)
    {
        memberAddressService.deleteLabelNew(reqVo);
        return getAllUserLabel(reqVo.getGp().getMemberId());
    }

    /**
     * 保存或更新收货地址
     */
    @ApiOperation("保存或更新收货地址")
    @PostMapping("saveOrUpdateAddress")
    public List<MemberAddressVo> saveOrUpdateAddress(@RequestBody MemberAddressVo reqVo)
    {
        memberAddressService.saveOrUpdateAddress(reqVo);
        MemberAddressNewRequestVo vo = new MemberAddressNewRequestVo();
        vo.setLat(reqVo.getLoginLat().toString());
        vo.setLon(reqVo.getLoginLon().toString());
        vo.setMemberId(reqVo.getMemberId());
        return getAllAddress(vo);
    }

    /**
     * 删除收货地址
     */
    @ApiOperation("删除收货地址")
    @PostMapping("deleteAddressNew")
    public List<MemberAddressVo> deleteAddressNew(@RequestBody MemberAddressIdRequestVo reqVo)
    {
        memberAddressService.deleteAddressNew(reqVo);
        MemberAddressNewRequestVo vo = new MemberAddressNewRequestVo();
        vo.setLat(reqVo.getLat());
        vo.setLon(reqVo.getLon());
        vo.setMemberId(reqVo.getGp().getMemberId());
        return getAllAddress(vo);
    }

    /**
     * 获取所有收货地址
     */
    @ApiOperation("获取所有收货地址")
    @GetMapping("getAllAddress")
    public List<MemberAddressVo> getAllAddress(@RequestBody MemberAddressNewRequestVo reqVo) { return memberAddressService.getAllAddress(reqVo); }

    /**
     * 查看收货地址附近有无门店
     */
    @ApiOperation("查看收货地址附近有无门店")
    @GetMapping("checkStore")
    public MemberAdddressCheckStoreResponseVo checkStore(@RequestBody BaseVo reqVo) { return null; }

    /**
     * 根据请求参数的会员id获取家 地址
     */
    @ApiOperation("根据请求参数的会员id获取家 地址")
    @GetMapping("findMemberAddressOfHome")
    public MemberAddressVo findMemberAddressOfHome(@RequestBody BaseRequestVo reqVo) { return memberAddressService.getShippingAddress(reqVo.getGp().getMemberId(), AddressTypes.home).get(0); }

    /**
     * 根据请求参数的会员id获取公司 地址
     */
    @ApiOperation("根据请求参数的会员id获取公司 地址")
    @GetMapping("findMemberAddressOfCompany")
    public MemberAddressVo findMemberAddressOfCompany(@RequestBody BaseRequestVo reqVo) { return memberAddressService.getShippingAddress(reqVo.getGp().getMemberId(), AddressTypes.company).get(0); }

    /**
     * 根据请求参数的会员id和关键字查询地址
     */
    @ApiOperation("根据请求参数的会员id和关键字查询地址")
    @GetMapping("findMemberAddressOnMemberIdAndKeyword")
    public MemberAddressVo findMemberAddressOnMemberIdAndKeyword(@RequestBody BaseRequestVo reqVo, @RequestParam("keyword") String keyword)  { return null; }

    /**
     * 根据id查询地址
     */
    @ApiOperation("根据id查询地址")
    @GetMapping("findMemberAddressById")
    public MemberAddressVo findMemberAddressById(@RequestBody BaseVo reqVo) { return memberAddressService.getShippingAddressById(reqVo.getId().toString()); }
    //********************************************
    //               address end
    //********************************************

    //********************************************
    //               notice start
    //********************************************
    @ApiOperation("根据id查询地址")
    @GetMapping("getAllNoticeRo")
    public List<NoticeVo> getAllNoticeRo(@RequestBody NoticePageRequestVo reqVo)
    {
        return noticeService.getAllNoticeRo(reqVo);
    }

    @ApiOperation("根据id查询地址")
    @GetMapping("getUserRemainNotices")
    public List<NoticeVo> getUserRemainNotices(@RequestBody NoticePageRequestVo reqVo)
    {
        return noticeService.getUserRemainNotices(reqVo.getUserId() == null ? reqVo.getUserId() : reqVo.getUserId(), reqVo.getSize());
    }
    //********************************************
    //               notice end


}


